package cn.felord.verse.generator;

import cn.felord.common.http.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * @author dax
 * @since 2022/2/12
 */
public final class MybatisGenerator {

    private MybatisGenerator() {
    }

    public static void coding(String url, String username, String password, String projectName, String packageName, String... tableNames) {
        final String mainDir = System.getProperty("user.dir") + "\\" + projectName + "\\src\\main\\";
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder ->
                        builder.author("system")
                                .outputDir(mainDir + "java")
                )
                .strategyConfig(builder ->
                        builder.addInclude(tableNames)
                                .entityBuilder()
                                .enableLombok()
                                .enableFileOverride()
                                .naming(NamingStrategy.underline_to_camel)
                                .idType(IdType.ASSIGN_ID)
                                .superClass(BaseEntity.class)
                                .enableTableFieldAnnotation()
                                .enableActiveRecord()
                                .addSuperEntityColumns("creator", "create_time", "updater", "update_time", "enabled")
                                .mapperBuilder()
                                .enableBaseResultMap()
                                .enableFileOverride()
                            /*    .serviceBuilder()
                                .enableFileOverride()*/
                )
                .packageConfig(builder ->
                        builder.parent(packageName)
                                .entity("entity")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, mainDir + "resources/mapper/"))
                )
                .templateConfig(builder ->
                        builder.disable(TemplateType.CONTROLLER)
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}
